home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / EmployeeDataManager.java < prev    next >
Text File  |  1998-11-01  |  12KB  |  369 lines

  1. /*
  2.     This simple extension of the java.awt.Frame class
  3.     contains all the elements necessary to act as the
  4.     main window of an application.
  5.  */
  6.  
  7. import java.awt.*;
  8. import java.io.*;
  9.  
  10.  
  11. import symantec.itools.awt.util.edit.SocialSecurityNumber;
  12. public class EmployeeDataManager extends Frame
  13. {
  14.     final static String employeeFileName = "employee.dat";
  15.     EmployeeFileAccessor efa;
  16.  
  17.     void updateEmployeeDisplay(EmployeeList elist)
  18.     {
  19.         Employee emp = elist.first();
  20.         String textdata = "";
  21.         for (;;)
  22.         {
  23.             if (emp == null) break;
  24.             System.out.println("Appending " + emp.toString() + " to textarea");
  25.             textdata += emp.toString();
  26.             emp = elist.next();
  27.         }
  28.         ta.setText(textdata);
  29.     }
  30.  
  31.     public EmployeeDataManager()
  32.     {
  33.         // This code is automatically generated by Visual Cafe when you add
  34.         // components to the visual environment. It instantiates and initializes
  35.         // the components. To modify the code, only use code syntax that matches
  36.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  37.         // parse your Java file into its visual environment.
  38.  
  39.         //{{INIT_CONTROLS
  40.         setLayout(new BorderLayout(0,0));
  41.         setBackground(java.awt.Color.lightGray);
  42.         setForeground(java.awt.Color.black);
  43.         setFont(new Font("Dialog", Font.PLAIN, 12));
  44.         setSize(751,309);
  45.         setVisible(false);
  46.         addPanel.setLayout(new GridBagLayout());
  47.         add("North", addPanel);
  48.         addPanel.setBounds(0,0,751,23);
  49.         label1.setText("Name:");
  50.         label1.setAlignment(java.awt.Label.RIGHT);
  51.         addPanel.add(label1, new com.symantec.itools.awt.GridBagConstraintsD(0,0,1,1,1.0,1.0,java.awt.GridBagConstraints.EAST,java.awt.GridBagConstraints.NONE,new Insets(0,0,0,0),0,0));
  52.         label1.setBounds(39,0,51,23);
  53.         addPanel.add(nfield, new com.symantec.itools.awt.GridBagConstraintsD(1,0,1,1,2.0,1.0,java.awt.GridBagConstraints.WEST,java.awt.GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));
  54.         nfield.setBounds(90,0,102,23);
  55.         label2.setText("Section#:");
  56.         label2.setAlignment(java.awt.Label.RIGHT);
  57.         addPanel.add(label2, new com.symantec.itools.awt.GridBagConstraintsD(2,0,1,1,1.0,1.0,java.awt.GridBagConstraints.EAST,java.awt.GridBagConstraints.NONE,new Insets(0,0,0,0),0,0));
  58.         label2.setBounds(231,0,65,23);
  59.         addPanel.add(afield, new com.symantec.itools.awt.GridBagConstraintsD(3,0,1,1,1.0,1.0,java.awt.GridBagConstraints.WEST,java.awt.GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0));
  60.         afield.setBounds(296,0,63,23);
  61.         label3.setText(" SSN:");
  62.         label3.setAlignment(java.awt.Label.RIGHT);
  63.         addPanel.add(label3, new com.symantec.itools.awt.GridBagConstraintsD(4,0,1,1,1.0,1.0,java.awt.GridBagConstraints.EAST,java.awt.GridBagConstraints.NONE,new Insets(0,0,0,0),0,0));
  64.         label3.setBounds(398,0,45,23);
  65.         sfield.setColumns(30);
  66.         sfield.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.TEXT_CURSOR));
  67.         addPanel.add(sfield, new com.symantec.itools.awt.GridBagConstraintsD(5,0,1,1,2.0,1.0,java.awt.GridBagConstraints.WEST,java.awt.GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),30,0));
  68.         sfield.setBackground(java.awt.Color.white);
  69.         sfield.setFont(new Font("Dialog", Font.PLAIN, 12));
  70.         sfield.setBounds(443,2,233,19);
  71.         addbutton.setLabel("Add");
  72.         addPanel.add(addbutton, new com.symantec.itools.awt.GridBagConstraintsD(6,0,1,1,1.0,1.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.NONE,new Insets(0,0,0,0),0,0));
  73.         addbutton.setBounds(695,0,35,23);
  74.         sortPanel.setLayout(new GridBagLayout());
  75.         add("Center", sortPanel);
  76.         sortPanel.setBounds(0,23,751,286);
  77.         choice.addItem("Name");
  78.         choice.addItem("Section");
  79.         choice.addItem("SSN");
  80.         try {
  81.             choice.select(0);
  82.         }
  83.         catch (IllegalArgumentException e) { }
  84.         sortPanel.add(choice, new com.symantec.itools.awt.GridBagConstraintsD(0,0,1,1,1.0,1.0,java.awt.GridBagConstraints.NORTH,java.awt.GridBagConstraints.HORIZONTAL,new Insets(30,10,0,0),0,0));
  85.         choice.setBackground(java.awt.Color.white);
  86.         choice.setBounds(10,30,129,25);
  87.         sortbutton.setLabel("Sort");
  88.         sortPanel.add(sortbutton, new com.symantec.itools.awt.GridBagConstraintsD(1,0,1,1,1.0,1.0,java.awt.GridBagConstraints.NORTH,java.awt.GridBagConstraints.NONE,new Insets(30,0,0,0),0,0));
  89.         sortbutton.setFont(new Font("Dialog", Font.PLAIN, 12));
  90.         sortbutton.setBounds(169,30,36,23);
  91.         sortPanel.add(ta, new com.symantec.itools.awt.GridBagConstraintsD(2,0,1,1,1.0,1.0,java.awt.GridBagConstraints.NORTH,java.awt.GridBagConstraints.BOTH,new Insets(30,0,40,15),0,0));
  92.         ta.setBounds(235,30,500,216);
  93.         setTitle("Employee Data Manager");
  94.         //}}
  95.  
  96.         //{{INIT_MENUS
  97.         menu1.setLabel("File");
  98.         menu1.add(miExit);
  99.         miExit.setLabel("Exit");
  100.         mainMenuBar.add(menu1);
  101.         menu3.setLabel("Help");
  102.         menu3.add(miAbout);
  103.         miAbout.setLabel("About..");
  104.         mainMenuBar.add(menu3);
  105.         mainMenuBar.setHelpMenu(menu3);
  106.         //$$ mainMenuBar.move(4,277);
  107.         setMenuBar(mainMenuBar);
  108.         //}}
  109.  
  110.         //{{REGISTER_LISTENERS
  111.         SymWindow aSymWindow = new SymWindow();
  112.         this.addWindowListener(aSymWindow);
  113.         SymAction lSymAction = new SymAction();
  114.         miAbout.addActionListener(lSymAction);
  115.         miExit.addActionListener(lSymAction);
  116.         addbutton.addActionListener(lSymAction);
  117.         sortbutton.addActionListener(lSymAction);
  118.         //}}
  119.  
  120.         System.out.println("Opening file");
  121.         try
  122.         {
  123.             efa = new EmployeeFileAccessor(employeeFileName);
  124.             System.out.println("File opened");
  125.         }
  126.         catch (IOException e)
  127.         {
  128.             System.out.println("Unable to open file " + employeeFileName);
  129.             return;
  130.         }
  131.  
  132.         // Fetch all employees from file, and display them in the text area
  133.         EmployeeList elist = efa.buildEmployeeList();
  134.         updateEmployeeDisplay(elist);
  135.  
  136.     }
  137.  
  138.     public EmployeeDataManager(String title)
  139.     {
  140.         this();
  141.         setTitle(title);
  142.     }
  143.  
  144.     public  void setVisible(boolean b)
  145.     {
  146.         if (b == true) {
  147.             setLocation(50, 50);
  148.         }
  149.         super.setVisible(b);
  150.     }
  151.  
  152.     static public void main(String args[])
  153.     {
  154.         (new EmployeeDataManager()).show();
  155.     }
  156.  
  157.     public void addNotify()
  158.     {
  159.         super.addNotify();
  160.  
  161.         if (fComponentsAdjusted)
  162.             return;
  163.  
  164.         // Adjust components according to the insets
  165.         setSize(getInsets().left + getInsets().right + getSize().width, getInsets().top + getInsets().bottom + getSize().height);
  166.         Component components[] = getComponents();
  167.         for (int i = 0; i < components.length; i++)
  168.         {
  169.             Point p = components[i].getLocation();
  170.             p.translate(getInsets().left, getInsets().top);
  171.             components[i].setLocation(p);
  172.         }
  173.         fComponentsAdjusted = true;
  174.     }
  175.  
  176.     boolean fComponentsAdjusted = false;
  177.  
  178.     //{{DECLARE_CONTROLS
  179.     java.awt.Panel addPanel = new java.awt.Panel();
  180.     java.awt.Label label1 = new java.awt.Label();
  181.     java.awt.TextField nfield = new java.awt.TextField();
  182.     java.awt.Label label2 = new java.awt.Label();
  183.     java.awt.TextField afield = new java.awt.TextField();
  184.     java.awt.Label label3 = new java.awt.Label();
  185.     symantec.itools.awt.util.edit.SocialSecurityNumber sfield = new symantec.itools.awt.util.edit.SocialSecurityNumber();
  186.     java.awt.Button addbutton = new java.awt.Button();
  187.     java.awt.Panel sortPanel = new java.awt.Panel();
  188.     java.awt.Choice choice = new java.awt.Choice();
  189.     java.awt.Button sortbutton = new java.awt.Button();
  190.     java.awt.TextArea ta = new java.awt.TextArea(5,0);
  191.     //}}
  192.  
  193.     //{{DECLARE_MENUS
  194.     java.awt.MenuBar mainMenuBar = new java.awt.MenuBar();
  195.     java.awt.Menu menu1 = new java.awt.Menu();
  196.     java.awt.MenuItem miExit = new java.awt.MenuItem();
  197.     java.awt.Menu menu3 = new java.awt.Menu();
  198.     java.awt.MenuItem miAbout = new java.awt.MenuItem();
  199.     //}}
  200.  
  201.     private void addEmployee()
  202.     {
  203.         Employee emp;
  204.  
  205.         // The ADD button was pressed; add new employee
  206.         System.out.println("Add an employee");
  207.  
  208.         // Create (and validate) an employee object
  209.         try    {
  210.             // get the SS Number String and add dashes if needed
  211.             String ssn = sfield.getText();
  212.             if (ssn.charAt(3) != '-')
  213.             {
  214.                 ssn = ssn.substring(0,3) + '-' + ssn.substring(3);
  215.             }
  216.             if (ssn.charAt(6) != '-')
  217.             {
  218.                 ssn = ssn.substring(0,6) + '-' + ssn.substring(6);            
  219.             }            
  220.             
  221.             emp = new Employee (nfield.getText(), afield.getText(), ssn);
  222.         } catch (Exception ex1)    {
  223.             System.out.println("Invalid employee data");
  224.             System.out.println(ex1.getMessage());
  225.             //showStatus("Invalid employee data; please re-enter");
  226.             return;
  227.         }
  228.  
  229.         // Add the employee to the file
  230.         try
  231.         {
  232.             efa.addEmployee(emp);
  233.         }
  234.         catch (IOException ex2)
  235.         {
  236.             System.out.println("Unable to add employee to file");
  237.             return;
  238.         }
  239.  
  240.         System.out.println("Employee added");
  241.  
  242.         // Redisplay the employee data
  243.         EmployeeList elist = efa.buildEmployeeList();
  244.         updateEmployeeDisplay(elist);
  245.     }
  246.  
  247.     private void sortEmployees()
  248.     {
  249.         // The SORT button was pressed; perform sort
  250.         EmployeeList elist = efa.buildEmployeeList();
  251.         elist.sort(choice.getSelectedIndex());
  252.         System.out.println("Sort on field " + choice.getSelectedIndex());
  253.  
  254.         // Redisplay the employee data
  255.         updateEmployeeDisplay(elist); 
  256.     }
  257.  
  258.     public void cleanupAndClose()
  259.     {
  260.         this.setVisible(false);
  261.         this.dispose();
  262.         System.exit(0); // close the application
  263.     }
  264.     
  265.     class SymWindow extends java.awt.event.WindowAdapter
  266.     {
  267.         public void windowClosing(java.awt.event.WindowEvent event)
  268.         {
  269.             Object object = event.getSource();
  270.             if (object == EmployeeDataManager.this)
  271.                 EmployeeDataManager_WindowClosing(event);
  272.         }
  273.     }
  274.  
  275.     void EmployeeDataManager_WindowClosing(java.awt.event.WindowEvent event)
  276.     {
  277.         // to do: code goes here.
  278.              
  279.         EmployeeDataManager_WindowClosing_Interaction1(event);
  280.     }
  281.  
  282.     void EmployeeDataManager_WindowClosing_Interaction1(java.awt.event.WindowEvent event)
  283.     {
  284.         try {
  285.             cleanupAndClose();
  286.         } catch (Exception e) {
  287.         }
  288.     }
  289.  
  290.     class SymAction implements java.awt.event.ActionListener
  291.     {
  292.         public void actionPerformed(java.awt.event.ActionEvent event)
  293.         {
  294.             Object object = event.getSource();
  295.             if (object == miAbout)
  296.                 miAbout_ActionPerformed(event);
  297.             else if (object == miExit)
  298.                 miExit_ActionPerformed(event);
  299.             else if (object == addbutton)
  300.                 addbutton_ActionPerformed(event);
  301.             else if (object == sortbutton)
  302.                 sortbutton_ActionPerformed(event);
  303.         }
  304.     }
  305.  
  306.     void miAbout_ActionPerformed(java.awt.event.ActionEvent event)
  307.     {
  308.         // to do: code goes here.
  309.              
  310.         miAbout_ActionPerformed_Interaction1(event);
  311.     }
  312.  
  313.     void miAbout_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  314.     {
  315.         try {
  316.             // AboutDialog Create and show as modal
  317.             (new AboutDialog(this, true)).setVisible(true);
  318.         } catch (Exception e) {
  319.         }
  320.     }
  321.  
  322.     void miExit_ActionPerformed(java.awt.event.ActionEvent event)
  323.     {
  324.         // to do: code goes here.
  325.              
  326.         miExit_ActionPerformed_Interaction1(event);
  327.     }
  328.  
  329.     void miExit_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  330.     {
  331.         try {
  332.             // QuitDialog Create and show as modal
  333.             (new QuitDialog(this, true)).setVisible(true);
  334.         } catch (Exception e) {
  335.         }
  336.     }
  337.  
  338.     void addbutton_ActionPerformed(java.awt.event.ActionEvent event)
  339.     {
  340.         // to do: code goes here.
  341.              
  342.         addbutton_ActionPerformed_Interaction1(event);
  343.     }
  344.  
  345.     void addbutton_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  346.     {
  347.         try {
  348.             this.addEmployee();
  349.         } catch (Exception e) {
  350.         }
  351.     }
  352.  
  353.     void sortbutton_ActionPerformed(java.awt.event.ActionEvent event)
  354.     {
  355.         // to do: code goes here.
  356.              
  357.         sortbutton_ActionPerformed_Interaction1(event);
  358.     }
  359.  
  360.     void sortbutton_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  361.     {
  362.         try {
  363.             this.sortEmployees();
  364.         } catch (Exception e) {
  365.         }
  366.     }
  367. }
  368.  
  369.